package com.vf.editor.mvc.scene.property;


import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.vf.editor.common.controller.BaseController;
import com.vf.editor.common.model.resources.SceneProperty;
import com.vf.editor.common.util.UuidUtil;

@Path(value="/scene/property", viewPath="/views/scene/property/")
public class PropertyController extends BaseController{
	
	@Inject
	private PropertyService srv;
	
	public void listData() {
		String thingId = getPara("thingId");
		List<SceneProperty> list = SceneProperty.dao.find("select * from "+SceneProperty.TABLE_NAME  +" where thingId='"+thingId+"' order by sort asc  ");
		renderPage(0,list, "", list.size());
	}
	
	public void save() {
		SceneProperty property = getModel(SceneProperty.class);
		if (StrKit.notBlank(property.getId())) {
			SceneProperty pro = SceneProperty.dao.findFirst("select * from "+SceneProperty.TABLE_NAME+" where thingId=? and code=? ",property.getThingId(),property.getCode());
			if(pro!=null && !StrKit.equals(property.getId(), pro.getId())) {
				renderError("属性标识（code）已存在！");
				return;
			}
			if (property.update()) {
				renderSuccess("更新成功！",property);
				srv.saveOrUpdateThing(property);
			} else {
				renderError("更新失败！");
			}
		} else {
			SceneProperty pro = SceneProperty.dao.findFirst("select * from "+SceneProperty.TABLE_NAME+" where thingId=? and code=? ",property.getThingId(),property.getCode());
			if(pro!=null) {
				renderError("属性标识（code）已存在！");
				return;
			}
			property.setId(UuidUtil.getUUID());
			if (property.save()) {
				renderSuccess("保存成功！",property);
				srv.saveOrUpdateThing(property);
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	public void delete() {
		String id = getPara("id");
		SceneProperty model = SceneProperty.dao.findById(id);
		if (model != null) {
			if (!model.delete()) {
				renderError("删除失败！");
				return;
			}
		}
		renderSuccess("删除成功！");
	}
	
}
